package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author :XingJiaCheng
 * @date :2022-2022/12/26-9:50
 * @description
 **/
@Slf4j
@Service
public class OrdersServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrdersService {
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartServices shoppingCartServices;

    @Override
    public void submit(Orders orders) {
        Long currentId = BaseContext.getCurrentId();
        log.info("当前用户id:{}", currentId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        List<ShoppingCart> cartList = shoppingCartServices.list(queryWrapper);
        if (cartList == null) {
            throw new CustomException("您的购物车位空请去添加");
        }

        AtomicInteger orderAmount = new AtomicInteger(0);
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        if (addressBook == null) {
            throw new CustomException("您还没有添加收货地址");
        }
        Long orderId  = IdWorker.getId();
        ArrayList<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(cart, orderDetail);
            orderDetail.setOrderId(orderId);
            orderDetailList.add(orderDetail);
            BigDecimal oneAmount = cart.getAmount().multiply(new BigDecimal(cart.getNumber()));
            orderAmount.addAndGet(oneAmount.intValue());
        }
        orders.setId(orderId);
        orders.setNumber("20221226"+orderId.toString());
        orders.setStatus(2);
        orders.setUserId(currentId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(new BigDecimal(orderAmount.intValue()));
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getProvinceName()+addressBook.getCityName());
        User user = userService.getById(currentId);
        orders.setUserName(user.getName());
        log.info("保存订单:{}",orders);
        this.save(orders);
        orderDetailService.saveBatch(orderDetailList);
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
        shoppingCartServices.remove(shoppingCartLambdaQueryWrapper);

//        Long currentId = BaseContext.getCurrentId();
//        log.info("用户id:{}", currentId);
//        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
//        List<ShoppingCart> list = shoppingCartServices.list(lambdaQueryWrapper);
    }
}
